package com.zw.mes.module.assemble.service;

import com.zw.mes.entity.asse.HalfStore;
import com.zw.mes.entity.shipments.AsseHalfStoreRecord;
import com.zw.mes.entity.sys.User;
import com.zw.mes.module.assemble.entity.AsseAssemblyBuy;
import com.zw.mes.module.assemble.entity.AsseAssemblyMake;
import com.zw.mes.module.assemble.entity.AsseAssemblySheet;
import com.zw.mes.module.assemble.entity.AsseStore;
import com.zw.mes.module.assemble.mapper.AssMakeMapper;
import com.zw.mes.module.assemble.model.MakeListHotModel;
import com.zw.mes.module.assemble.model.MakeListModel;
import com.zw.mes.module.assemble.repository.AsseAssemblyBuyRepository;
import com.zw.mes.module.assemble.repository.AsseAssemblyMakeRepository;
import com.zw.mes.module.assemble.repository.AsseAssemblySheetRepository;
import com.zw.mes.module.assemble.repository.AsseHalfStoreRepository;
import com.zw.mes.module.shipments.repository.AsseHalfStoreRecordRepository;
import com.zw.mes.module.system.service.LogService;
import com.zw.mes.util.BigDecimalUtils;
import com.zw.mes.util.ShiroUtils;
import lombok.var;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Description: 装配派工单 制作件及外购件匹配 业务
 * @Author: China.wsg
 * @Date: 2021/05/06 13:28
 */
@Service
@SuppressWarnings("all")
public class AssMakeService {

    @Autowired
    private AssMakeMapper makeMapper;
    @Autowired
    private LogService logService;
    @Autowired
    private AsseAssemblySheetRepository assemblySheetRepository;
    @Autowired
    private AsseAssemblyMakeRepository assemblyMakeRepository;
    @Autowired
    private AsseAssemblyBuyRepository assemblyBuyRepository;
    @Autowired
    private AsseHalfStoreRecordRepository halfStoreRecordRepository;
    @Autowired
    private AsseHalfStoreRepository asseHalfStoreRepository;

    /**
     * @Author wsg
     * @Date 13:29 2021/5/6
     * @Description 装配派工单查询列表（排除类型为‘自动化’数据）
     **/
    public List<AsseAssemblySheet> findAssemblySheetByQuery(AsseAssemblySheet assemblySheet) {
        return makeMapper.findAssemblySheetByQuery(assemblySheet);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/6
     * @Description 制作件列表
     **/
    public List<AsseAssemblyMake> findAsseAssemblyMakeList(AsseAssemblyMake assemblyMake) {
        return makeMapper.findAsseAssemblyMakeList(assemblyMake);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/6
     * @Description 外购件列表
     **/
    public List<AsseAssemblyBuy> findAsseAssemblyBuyList(Integer sheetId) {
        return makeMapper.findAsseAssemblyBuyList(sheetId);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/6
     * @Description 装配零件信息
     **/
    public List<AsseAssemblyMake> findMakeChooseList(HalfStore halfStore) {
        return makeMapper.findMakeChooseList(halfStore);
    }

    /**
     * @Author wsg
     * @Date 15:29 2021/5/7
     * @Description 添加装配零件
     **/
    @Transactional(rollbackFor = Exception.class)
    public int addMake(String arrIds, Integer sheetId) {

        AsseAssemblyMake assemblyMake = new AsseAssemblyMake();
        assemblyMake.setAsseAssemblySheetId(sheetId);
        User user = ShiroUtils.getUser();
        assemblyMake.setCreateCode(user.getAccount());
        assemblyMake.setCreateName(user.getName());
        assemblyMake.setCreateTime(new Date());
        assemblyMake.setIds(arrIds);
        int rows = makeMapper.insertAssemblyMake(assemblyMake);

        AsseAssemblySheet assemblySheet = assemblySheetRepository.getOne(sheetId);
        logService.saveLog("派工单领料计划添加", "派工单ID：" + sheetId + "，生产令号：" + assemblySheet.getProdNo()
                + "<br>项目名称：" + assemblySheet.getProName() + "<br>类型：" + assemblySheet.getSpecial() + "<br>部分：" + assemblySheet.getPart()
                + "<br>装配图号：" + assemblySheet.getDrawNo() + "<br>装配图名：" + assemblySheet.getDrawName() + "<br>添加零件：" + rows + "条"
                + "<br>零件半成品库ID集合：" + arrIds
        );
        return rows;
    }

    /**
     * @Author wsg
     * @Date 13:29 2021/5/7
     * @Description 删除装配零件
     **/
    @Transactional(rollbackFor = Exception.class)
    public int delMake(String arrIds, Integer sheetId) {
        // 判断：已被提交的数据不允许删除
        Integer cn = makeMapper.getMakeCommitCountByMakeIds(arrIds);
        if (cn.compareTo(0) > 0) {
            return 0;
        }
        int rows = makeMapper.deleteAssemblyMake(arrIds);

        AsseAssemblySheet assemblySheet = assemblySheetRepository.getOne(sheetId);
        logService.saveLog("派工单领料计划删除", "派工单ID：" + sheetId + "，生产令号：" + assemblySheet.getProdNo()
                + "<br>项目名称：" + assemblySheet.getProName() + "<br>类型：" + assemblySheet.getSpecial() + "<br>部分：" + assemblySheet.getPart()
                + "<br>装配图号：" + assemblySheet.getDrawNo() + "<br>装配图名：" + assemblySheet.getDrawName() + "<br>删除零件：" + rows + "条"
                + "<br>装配单制作零件表ID集合：" + arrIds
        );
        return rows;
    }

    /**
     * @Author wsg
     * @Date 15:43 2021/5/7
     * @Description 修改零件领用数量
     **/
    @Transactional(rollbackFor = Exception.class)
    public int editMakeUseNum(AsseAssemblyMake assemblyMake) {
        // 判断：已被提交的数据不允许删除
        Integer cn = makeMapper.getMakeCommitCountByMakeIds(assemblyMake.getId().toString());
        if (cn.compareTo(0) > 0) {
            return 0;
        }
        AsseAssemblyMake makeResult = assemblyMakeRepository.getOne(assemblyMake.getId());
        int oldUseNum = makeResult.getUseNum();// 改前数量
        makeResult.setUseNum(assemblyMake.getUseNum());
        assemblyMakeRepository.save(makeResult);

        AsseAssemblySheet assemblySheet = assemblySheetRepository.getOne(makeResult.getAsseAssemblySheetId());
        logService.saveLog("派工单领料数量修改", "派工单ID：" + assemblySheet.getId() + "，生产令号：" + assemblySheet.getProdNo()
                + "<br>项目名称：" + assemblySheet.getProName() + "<br>类型：" + assemblySheet.getSpecial() + "<br>部分：" + assemblySheet.getPart()
                + "<br>领用图号：" + makeResult.getDrawNo() + "<br>领用图名：" + makeResult.getDrawName() + "<br>领用数量：" + oldUseNum
                + "->" + assemblyMake.getUseNum()
        );
        return 1;
    }

    /**
     * @Author wsg
     * @Date 10:15 2021/5/8
     * @Description 保存装配外购件信息
     **/
    @Transactional(rollbackFor = Exception.class)
    public int saveBuy(Integer sheetId, String[] matNameDesc, String[] trackCode, Integer[] useNum
            , String[] unit, String[] backStatus, String[] position, String[] comment) {

        AsseAssemblySheet assemblySheet = assemblySheetRepository.getOne(sheetId);

        // 根据主单ID，删除外购件明细
        makeMapper.deleteAssemblyBuyBySheetId(sheetId);

        // 如果保存数据为空，直接清空原有数据即可
        if (matNameDesc == null) {
            logService.saveLog("保存装配外购件信息", "派工单ID：" + sheetId + "，生产令号：" + assemblySheet.getProdNo()
                    + "<br>项目名称：" + assemblySheet.getProName() + "<br>类型：" + assemblySheet.getSpecial() + "<br>部分：" + assemblySheet.getPart()
                    + "<br>装配图号：" + assemblySheet.getDrawNo() + "<br>装配图名：" + assemblySheet.getDrawName() + "<br>装配外购件：0条");
            return 1;
        }
        // 重新插入外购件明细
        Date dateTime = new Date();
        for (int i = 0; i < matNameDesc.length; i++) {
            AsseAssemblyBuy assemblyBuy = new AsseAssemblyBuy();
            assemblyBuy.setAsseAssemblySheetId(sheetId);
            assemblyBuy.setMatNameDesc(matNameDesc[i]);
            if (trackCode.length > 0) {
                assemblyBuy.setTrackCode(trackCode[i]);
            }
            assemblyBuy.setUseNum(useNum[i]);
            assemblyBuy.setUnit(unit[i]);
            assemblyBuy.setBackStatus(backStatus[i]);
            if (position.length > 0) {
                assemblyBuy.setPosition(position[i]);
            }
            if (comment.length > 0) {
                assemblyBuy.setComment(comment[i]);
            }
            User user = ShiroUtils.getUser();
            assemblyBuy.setCreateCode(user.getAccount());
            assemblyBuy.setCreateName(user.getName());
            assemblyBuy.setCreateTime(dateTime);
            assemblyBuy = assemblyBuyRepository.save(assemblyBuy);
        }

        logService.saveLog("保存装配外购件信息", "派工单ID：" + sheetId + "，生产令号：" + assemblySheet.getProdNo()
                + "<br>项目名称：" + assemblySheet.getProName() + "<br>类型：" + assemblySheet.getSpecial() + "<br>部分：" + assemblySheet.getPart()
                + "<br>装配图号：" + assemblySheet.getDrawNo() + "<br>装配图名：" + assemblySheet.getDrawName() + "<br>装配外购件：" + matNameDesc.length + "条"
        );
        return 1;
    }

    /**
     * @param makeListModel
     * @Author wsg
     * @Date 13:46 2021/5/7
     * @Description 派工领料计划列表
     **/
    public List<MakeListModel> findAsseAssemblyMakeListByQuery(MakeListModel makeListModel) {
        return makeMapper.findAsseAssemblyMakeListByQuery(makeListModel);
    }

    /**
     * @param arrIds 批量提交明细ID集合
     * @Author wsg
     * @Date 15:29 2021/5/8
     * @Description 提交装配零件
     **/
    @Transactional(rollbackFor = Exception.class)
    public String subMake(String arrIds) {
        // 判断：已被提交的数据不允许提交
        Integer cn = makeMapper.getMakeCommitCountByMakeIds(arrIds);
        if (cn.compareTo(0) > 0) {
            return "被提交";
        }
        // 判断：库存数量是否充足
        Integer cw = makeMapper.getMakeCommitMinusCountByMakeIds(arrIds);
        if (cw.compareTo(0) > 0) {
            return "库存不足";
        }

        /** 更新提交状态 2=已提交 **/
        int rows = makeMapper.updateAssemblyMakeStatus(arrIds, 2, new Date());
        /** 半成品库数量减少出库 **/
        makeMapper.updateMinusHalfStore(arrIds);

        /** 循环插入出库记录 **/
        String[] arrayIds = arrIds.split("\\,");
        for (int i = 0; i < arrayIds.length; i++) {
            AsseHalfStoreRecord halfStoreRecord = new AsseHalfStoreRecord();
            halfStoreRecord = makeMapper.findInsertHalfStoreRecordInfoByMakeId(Integer.valueOf(arrayIds[i]));
            halfStoreRecord.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
            halfStoreRecord = halfStoreRecordRepository.save(halfStoreRecord);
            // 插入出库记录 同时 更新装配单制作零件表 recordId 字段
            AsseAssemblyMake assemblyMake = assemblyMakeRepository.getOne(Integer.valueOf(arrayIds[i]));
            assemblyMake.setRecordId(halfStoreRecord.getId());
            assemblyMakeRepository.save(assemblyMake);
        }
        logService.saveLog("派工单领料计划提交", "提交零件：" + rows + "条" + "<br>零件表ID集合：" + arrIds);
        return "提交成功";
    }

    /**
     * @param sheetId 派工单ID
     * @Author wsg
     * @Date 09:29 2021/5/12
     * @Description 提交该单据未提交的装配零件
     **/
    @Transactional(rollbackFor = Exception.class)
    public String subMakeSheet(Integer sheetId) {
        // 判断：库存数量是否充足
        Integer cw = makeMapper.getMakeCommitMinusCountBySheetId(sheetId);
        if (cw.compareTo(0) > 0) {
            return "库存不足";
        }

        /** 取得要提交的制作件Id集合 **/
        List<Integer> ids = makeMapper.findNotCommitMakeIdListBySheetId(sheetId);
        if (ids.size() == 0) {
            return "提交成功";
        }
        String arrIds = StringUtils.join(ids.toArray(), ",");
        /** 更新提交状态 2=已提交 **/
        int rows = makeMapper.updateAssemblyMakeStatus(arrIds, 2, new Date());
        /** 半成品库数量减少出库 **/
        makeMapper.updateMinusHalfStore(arrIds);

        /** 循环插入出库记录 **/
        String[] arrayIds = arrIds.split("\\,");
        for (int i = 0; i < arrayIds.length; i++) {
            AsseHalfStoreRecord halfStoreRecord = new AsseHalfStoreRecord();
            halfStoreRecord = makeMapper.findInsertHalfStoreRecordInfoByMakeId(Integer.valueOf(arrayIds[i]));
            halfStoreRecord.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
            halfStoreRecord = halfStoreRecordRepository.save(halfStoreRecord);
            // 更新装配单制作零件表 recordId 字段
            AsseAssemblyMake assemblyMake = assemblyMakeRepository.getOne(Integer.valueOf(arrayIds[i]));
            assemblyMake.setRecordId(halfStoreRecord.getId());
            assemblyMakeRepository.save(assemblyMake);
        }
        logService.saveLog("派工单领料计划保存", "保存零件：" + rows + "条" + "<br>零件表ID集合：" + arrIds);
        return "提交成功";
    }

    /**
     * @param id 撤销件主键
     * @Author wsg
     * @Date 15:29 2021/5/10
     * @Description 撤销装配零件
     **/
    @Transactional(rollbackFor = Exception.class)
    public int cancelMake(Integer id) {

        AsseAssemblyMake make = assemblyMakeRepository.getOne(id);
        // 校验：已撤销不再允许撤销
        if (make.getStatus() == 1) {
            return 0;
        }

        /** 更新提交状态 1=未提交 **/
        int rows = makeMapper.updateAssemblyMakeStatus(id.toString(), 1, null);
        /** 半成品库数量增加出库数量 **/
        makeMapper.updatePlusHalfStore(id.toString());
        /** 插入出库红冲记录 **/
        makeMapper.insertHalfStoreRecordRed(id.toString(), ShiroUtils.getUser().getAccount());
        logService.saveLog("派工单领料计划撤销", "撤销零件：" + rows + "条" + "<br>零件表ID集合：" + id);
        return 1;
    }

    /**
     * @Author wsg
     * @Date 16:29 2021/5/20
     * @Description 保存装配零件信息列表 hot控件
     **/
    @Transactional(rollbackFor = Exception.class)
    public String saveMakeHotList(MakeListHotModel makeListHotModel) {
        String msg = "";  // 返回提示信息

        // 1 校验保存数据可用性、存在不满足条件数据 整单不允许保存
        msg = checkOutValidate(makeListHotModel);
        if (msg.length() > 0) {
            msg = msg + "<br>保存失败！";
            return msg;
        }
        // 2 保存单据明细信息和库存在库(出库)
        String addIds = saveOutOrderDetail(makeListHotModel);// 新增Ids

        // 3 生成操作日志
        String delIds = makeListHotModel.getDelData() != null ? StringUtils.join(makeListHotModel.getDelData().toArray(), ",") : ""; // 删除Ids
        String updIds = ""; // 修改Ids
        if (makeListHotModel.getUpdateData() != null) {
            List<Integer> updIdList = new ArrayList<>();
            for (int i = 0; i < makeListHotModel.getUpdateData().size(); i++) {
                updIdList.add(makeListHotModel.getUpdateData().get(i).getId());
            }
            updIds = StringUtils.join(updIdList.toArray(), ",");
        }
        logService.saveLog("装配零件信息保存", "新增零件ID集合：" + addIds + "<br>修改零件ID集合：" + updIds + "<br>删除零件ID集合：" + delIds);
        return makeListHotModel.getFormData().getAsseAssemblySheetId() == null ? "保存失败" : "保存成功";
    }

    /**
     * @Author wsg
     * @Date 13:29 2021/5/21
     * @Description 校验出库单数据可用性
     **/
    private String checkOutValidate(MakeListHotModel makeListHotModel) {

        // 循环判断可用在库库存量是否充足（提示内容：图名 可用在库库存量不足，保存失败！）
        HalfStore halfStore;
        AsseAssemblyMake orderDetailModel;
        List<AsseAssemblyMake> orderDetailModelList = new ArrayList<>();
        String msg1 = "";
        String msg2 = "";
        String msg3 = "";

        if (makeListHotModel.getInsertData() != null) {
            /** 遍历插入：统计同一半成品总出库总量 **/
            for (int i = 0; i < makeListHotModel.getInsertData().size(); i++) {
                if (makeListHotModel.getInsertData().get(i).getAsseHalfStoreId() == null) {
                    continue;
                }

                // 同张单据多条数据相同时，需要将出库数量累加之后再进行库存是否充足校验
                boolean sameFlg = false;
                for (int j = 0; j < orderDetailModelList.size(); j++) {

                    if (sameFlg) {
                        break;
                    }

                    Integer insertDetailId = makeListHotModel.getInsertData().get(i).getAsseHalfStoreId();
                    Integer outDetailId = orderDetailModelList.get(j).getAsseHalfStoreId();
                    if (insertDetailId.intValue() == outDetailId.intValue()) {
                        orderDetailModelList.get(j).setUseNum(orderDetailModelList.get(j).getUseNum() + makeListHotModel.getInsertData().get(i).getUseNum());
                        sameFlg = true;
                    }
                }

                if (!sameFlg) {
                    orderDetailModel = new AsseAssemblyMake();
                    orderDetailModel.setAsseHalfStoreId(makeListHotModel.getInsertData().get(i).getAsseHalfStoreId());
                    orderDetailModel.setUseNum(makeListHotModel.getInsertData().get(i).getUseNum());
                    orderDetailModelList.add(orderDetailModel);
                }
            }
        }

        if (makeListHotModel.getUpdateData() != null) {
            /** 遍历修改 **/
            for (int i = 0; i < makeListHotModel.getUpdateData().size(); i++) {
                // 将净出库量（本次出库量-上次出库量）大于零的添加到orderDetailModelList比对列表中
                AsseAssemblyMake updDate = makeListHotModel.getUpdateData().get(i);
                // 数据库最近数据
                AsseAssemblyMake oldDate = assemblyMakeRepository.getOne(makeListHotModel.getUpdateData().get(i).getId());
                String wrongInfo = oldDate.getDrawName();
                // 校验：已经被修改的数据 不允许保存
                if (updDate.getOldUseNum().intValue() != oldDate.getUseNum().intValue()) {
                    msg1 = msg1 + "<br>" + wrongInfo;
                    continue;
                }
                // 校验：已经被删除的数据 不允许保存
                if (oldDate.getIsDel().compareTo(1) == 0 || oldDate.getStatus().compareTo(1) == 0) {
                    msg1 = msg1 + "<br>" + wrongInfo;
                    continue;
                }
                int outNum = updDate.getUseNum() - oldDate.getUseNum();
                if (outNum > 0) {
                    orderDetailModel = new AsseAssemblyMake();
                    orderDetailModel.setAsseHalfStoreId(makeListHotModel.getUpdateData().get(i).getAsseHalfStoreId());
                    orderDetailModel.setUseNum(outNum);
                    orderDetailModelList.add(orderDetailModel);
                }
            }
            if (msg1.length() > 0) {
                msg1 = msg1 + "<br>以上数据已被其他人修改";
            }
        }

        if (makeListHotModel.getDelData() != null) {
            /** 遍历删除 **/
            for (var i = 0; i < makeListHotModel.getDelData().size(); i++) {
                AsseAssemblyMake make = assemblyMakeRepository.getOne(makeListHotModel.getDelData().get(i));
                // 校验：已撤销不再允许撤销
                if (make.getStatus().compareTo(1) == 0 || make.getIsDel().compareTo(1) == 0) {
                    msg2 = msg2 + "<br>" + make.getDrawName();
                    continue;
                }
            }
            if (msg2.length() > 0) {
                msg2 = msg2 + "<br>以上数据已被其他人删除";
            }
        }

        // 校验：库存不足
        for (var i = 0; i < orderDetailModelList.size(); i++) {
            // 取得原料在库明细信息，与实际出库量进行比对，判断库存是否充足
            halfStore = asseHalfStoreRepository.getOne(orderDetailModelList.get(i).getAsseHalfStoreId());
            int a = halfStore.getNum();
            int c = orderDetailModelList.get(i).getUseNum();
            if (a < c) {
                String wrongInfo = halfStore.getDrawName();
                msg3 = msg3 + "<br>" + wrongInfo;
            }
        }
        if (msg3.length() > 0) {
            msg3 = msg3 + "<br>以上数据可用在库库存量不足";
        }
        return msg1 + msg2 + msg3;
    }

    /**
     * @return 返回插入制作件ID集合
     * @Author wsg
     * @Date 13:29 2021/5/22
     * @Description 保存单据明细信息和库存在库(出库)，三类数据分别处理（新增、修改、删除）
     **/
    private String saveOutOrderDetail(MakeListHotModel makeListHotModel) {

        /** 删除出库明细 **/
        List<Integer> makeIdsDel = makeListHotModel.getDelData();
        if (makeIdsDel != null) {
            for (int i = 0; i < makeIdsDel.size(); i++) {
                // 更新提交状态 1=未提交
                makeMapper.updateAssemblyMakeStatus(makeIdsDel.get(i).toString(), 1, null);
                // 半成品库数量增加出库数量
                makeMapper.updatePlusHalfStore(makeIdsDel.get(i).toString());
                // 插入出库红冲记录
                makeMapper.insertHalfStoreRecordRed(makeIdsDel.get(i).toString(), ShiroUtils.getUser().getAccount());
                // 撤销并删除装配件数据
                makeMapper.updateCancelAndDelMake(makeIdsDel.get(i));
            }
        }

        /** 修改出库明细 **/
        List<AsseAssemblyMake> makeUpds = makeListHotModel.getUpdateData();
        if (makeUpds != null) {
            for (int i = 0; i < makeUpds.size(); i++) {
                String makeId = makeUpds.get(i).getId().toString();// 被修改制作件ID
                /** 1先做冲单流程 **/
                // 更新提交状态 1=未提交
                makeMapper.updateAssemblyMakeStatus(makeId, 1, null);
                // 插入出库红冲记录
                makeMapper.insertHalfStoreRecordRed(makeId, ShiroUtils.getUser().getAccount());
                /** 2再做出库流程 **/
                // 修改制作件出库数量
                AsseAssemblyMake makeResult = assemblyMakeRepository.getOne(makeUpds.get(i).getId());
                makeResult.setUseNum(makeUpds.get(i).getUseNum());
                assemblyMakeRepository.save(makeResult);
                // 更新提交状态 2=已提交
                makeMapper.updateAssemblyMakeStatus(makeId, 2, new Date());
                // 更新库存在库数量
                Integer outNum = makeUpds.get(i).getUseNum() - makeUpds.get(i).getOldUseNum();// 净出库量(净出库量可能是负数，正常)
                makeMapper.updateHalfStoreNum(makeResult.getAsseHalfStoreId(), outNum);
                // 插入出库记录 同时 更新装配单制作零件表 recordId 字段
                AsseHalfStoreRecord halfStoreRecord = new AsseHalfStoreRecord();
                halfStoreRecord = makeMapper.findInsertHalfStoreRecordInfoByMakeId(Integer.valueOf(makeId));
                halfStoreRecord.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
                halfStoreRecord.setDrawNum(makeUpds.get(i).getUseNum());
                halfStoreRecord = halfStoreRecordRepository.save(halfStoreRecord);
                AsseAssemblyMake assemblyMake = assemblyMakeRepository.getOne(Integer.valueOf(makeId));
                assemblyMake.setRecordId(halfStoreRecord.getId());
                assemblyMakeRepository.save(assemblyMake);
            }
        }

        /** 新增出库明细 **/
        List<AsseAssemblyMake> makeAdds = makeListHotModel.getInsertData();
        List<Integer> addIdList = new ArrayList<>();
        if (makeAdds != null) {
            for (int i = 0; i < makeAdds.size(); i++) {
                // 插入制作件清单表
                AsseAssemblyMake assemblyMake = new AsseAssemblyMake();
                assemblyMake.setAsseAssemblySheetId(makeListHotModel.getFormData().getAsseAssemblySheetId());
                assemblyMake.setUseNum(makeAdds.get(i).getUseNum());
                User user = ShiroUtils.getUser();
                assemblyMake.setCreateCode(user.getAccount());
                assemblyMake.setCreateName(user.getName());
                assemblyMake.setCreateTime(new Date());
                assemblyMake.setCommitTime(new Date());
                assemblyMake.setIds(makeAdds.get(i).getAsseHalfStoreId().toString());
                makeMapper.insertAssemblyMake(assemblyMake);
                // 半成品库数量减少出库
                makeMapper.updateMinusHalfStore(assemblyMake.getId().toString());
                // 插入出库记录 同时 更新装配单制作零件表 recordId 字段
                AsseHalfStoreRecord halfStoreRecord = new AsseHalfStoreRecord();
                halfStoreRecord = makeMapper.findInsertHalfStoreRecordInfoByMakeId(Integer.valueOf(assemblyMake.getId()));
                halfStoreRecord.setStorageRegisterCode(ShiroUtils.getUser().getAccount());
                halfStoreRecord = halfStoreRecordRepository.save(halfStoreRecord);
                AsseAssemblyMake assemblyMake2 = assemblyMakeRepository.getOne(assemblyMake.getId());
                assemblyMake2.setRecordId(halfStoreRecord.getId());
                assemblyMakeRepository.save(assemblyMake2);

                // 保留添加数据ID，操作日志用
                addIdList.add(assemblyMake.getId());
            }
        }
        return StringUtils.join(addIdList.toArray(), ",");
    }
}
